package com.youlu.campus.website.service.impl;

import com.youlu.campus.website.common.enums.PlatformType;
import com.youlu.campus.website.entity.ActivityDescription;
import com.youlu.campus.website.entity.ActivityDescriptionPageQuery;
import com.youlu.campus.website.entity.ActivityDescriptionTypeVo;
import com.youlu.campus.website.entity.ActivityType;
import com.youlu.campus.website.service.ActivityDescriptionService;
import com.youlu.campus.website.service.ActivityDescriptionTypeService;
import com.youlu.campus.website.service.ActivityTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 活动简介与类型服务
 *
 * @Description: ActivityInfoTypeServiceImpl
 * @Date 2025/8/22
 * @module ActivityInfoTypeServiceImpl
 * @author: su
 */
@Slf4j
@Service
public class ActivityDescriptionTypeServiceImpl implements ActivityDescriptionTypeService {

    @Autowired
    private ActivityTypeService activityTypeService;

    @Autowired
    private ActivityDescriptionService activityDescriptionService;

    @Override
    public List<ActivityDescriptionTypeVo> listEnabledType(PlatformType platform) {
        ActivityDescriptionPageQuery pageQuery = new ActivityDescriptionPageQuery();
        pageQuery.setPlatform(platform);
        pageQuery.setEnabled(true);
        pageQuery.setPage(1);
        pageQuery.setPageSize(1000);//获取全部活动
        Page<ActivityDescription> page = activityDescriptionService.findByPageToWeb(pageQuery);
        List<ActivityDescription> activityDescriptions = page.getContent();
        if (CollectionUtils.isEmpty(activityDescriptions)) {
            return Collections.emptyList();
        }
        //根据类型分组
        Map<String, List<ActivityDescription>> activityDescriptionMap = activityDescriptions.stream().collect(Collectors.groupingBy(ActivityDescription::getType));

        ArrayList<String> types = new ArrayList<>(activityDescriptionMap.keySet());

        List<ActivityType> activityTypes = activityTypeService.findByTypeIn(types);
        if (CollectionUtils.isEmpty(activityTypes)) {
            return Collections.emptyList();
        }
        if (activityTypes.size() > 5) {
            activityTypes = activityTypes.subList(0, 5);
        }
        List<ActivityDescriptionTypeVo> list = new ArrayList<>();
        for (ActivityType activityType : activityTypes) {
            ActivityDescriptionTypeVo vo = new ActivityDescriptionTypeVo();
            vo.setId(activityType.getId());
            vo.setType(activityType.getType());
            vo.setName(activityType.getName());
            vo.setDescription(activityType.getDescription());
            vo.setSort(activityType.getSort());
            if (activityDescriptionMap != null && activityDescriptionMap.containsKey(activityType.getType())) {
                List<ActivityDescription> descriptions = activityDescriptionMap.get(activityType.getType());
                if (descriptions.size() > 4) {
                    descriptions = descriptions.subList(0, 4);
                }
                vo.setActivityDescriptions(descriptions);
            }
            list.add(vo);

        }
        return list;
    }
}
